package com.cloud.sso.web;

import com.cloud.sso.common.constant.OperateType;
import com.cloud.sso.common.exception.AppException;
import com.cloud.sso.common.log.annoatation.BusinessLog;
import com.cloud.sso.common.page.AppPageInfo;
import com.cloud.sso.common.result.Result;
import com.cloud.sso.dto.MenuQueryDto;
import com.cloud.sso.dto.MenuSaveDto;
import com.cloud.sso.entity.SysMenu;
import com.cloud.sso.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 菜单处理类
 */
@RestController
@RequestMapping("/api/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 分页查询菜单
     *
     * @param dto *
     * @return
     */
    @RequestMapping("/query")
    public Result<AppPageInfo> query(@RequestBody MenuQueryDto dto) {
        return Result.success(menuService.query(dto));
    }

    /**
     * 保存菜单 *
     *
     * @param dto
     * @return
     */
    @BusinessLog(value = "新增菜单", module = "菜单管理", field = "menuName", type = OperateType.INSERT)
    @RequestMapping("/save")
    public Result save(@RequestBody @Validated MenuSaveDto dto, BindingResult result) {
        if (result.hasErrors()) {
            throw new AppException(result.getFieldError().getDefaultMessage());
        }

        menuService.save(dto);
        return Result.success(1);
    }

    /**
     * 更新菜单
     *
     * @param dto *
     * @return
     */
    @BusinessLog(value = "修改菜单", module = "菜单管理", field = "menuName", type = OperateType.UPDATE)
    @RequestMapping("/update")
    public Result insert(@RequestBody MenuSaveDto dto) {
        menuService.save(dto);
        return Result.success(1);
    }

    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    @BusinessLog(value = "删除菜单", module = "菜单管理", field = "id", type = OperateType.DELETE)
    @RequestMapping("/delete")
    public Result insert(@RequestParam String id) {
        menuService.delete(id);
        return Result.success(1);
    }

    /**
     * 查询菜单详情 *
     *
     * @param id
     * @return
     */
    @RequestMapping("/get")
    public Result get(@RequestParam String id) {
        return Result.success(menuService.get(id));
    }

    /**
     * 查询应用的树状菜单
     *
     * @param appId
     * @return
     */
    @RequestMapping("/tree")
    public Result menuTree(@RequestParam String appId) {
        return Result.success(menuService.tree(appId));
    }

    /**
     * 查询子菜单
     *
     * @param appId
     * @param parentId
     * @return
     */
    @RequestMapping("/sub")
    public Result subMenu(@RequestParam String appId,
                          @RequestParam String parentId) {
        return Result.success(menuService.subMenu(appId, parentId));
    }


}
